package cn.iocoder.yudao.module.trade.service.order.handler;

import cn.iocoder.yudao.framework.common.enums.trade.TransactionTypeEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.module.member.api.config.dto.MemberConfMaterialDTO;
import cn.iocoder.yudao.module.member.api.config.dto.MemberConfRespDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyTypeEnum;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderOrderReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.ORDER_CREATE_FAIL_USER_NO_PERMISSION;

/**
 * 提料交易模式处理器
 */
@Component
public class TakeMaterialTransactionHandler extends AbstractTransactionModeHandler {

    private static final TransactionTypeEnum TRANSACTION_TYPE = TransactionTypeEnum.TAKE_METAL;

    @Override
    public Integer getTransactionType() {
        return TRANSACTION_TYPE.getType();
    }

    @Override
    public void validatePermission(MemberConfRespDTO memberConf) {
        if (Boolean.FALSE.equals(memberConf.getFullPayBuy())) {
            throw ServiceExceptionUtil.exception(ORDER_CREATE_FAIL_USER_NO_PERMISSION, TRANSACTION_TYPE.getDesc());
        }
    }
    
    @Override
    public void validateTradeLimits(MemberConfMaterialDTO materialConf, AppTradeOrderOrderReqVO settlementReqVO) {
        // 调用父类的通用限制校验
        super.validateTradeLimits(materialConf, settlementReqVO);
        
        // 提料特有校验 - 例如验证用户存料余额是否充足
        // TODO: 待实现根据用户存料余额验证
    }

    @Override
    protected BigDecimal calculatePayPrice(TradePriceCalculateReqBO calculateReqBO, TradePriceCalculateRespBO calContext) {
        return calculateReqBO.getWeight();
    }

    @Override
    protected void setPriceResult(TradePriceCalculateReqBO calculateReqBO, TradePriceCalculateRespBO result, TradePriceCalculateRespBO context) {
        if (context.getProducePrice().compareTo(BigDecimal.ZERO) > 0) {
            result.setPayType(PayNotifyTypeEnum.ORDER_PROCESS.getType());
        } else {
            result.setPayType(PayNotifyTypeEnum.ORDER.getType());
        }
        result.setProducePrice(context.getProducePrice());
    }

    @Override
    public PayOrderCreateReqDTO creatSettlePay(TradeOrderDO order, Boolean isViolate) {
        return null;
    }

} 